package common.dao.orient;


import com.orientechnologies.orient.jdbc.OrientJdbcConnection;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * Created by common on 2017/3/17.
 */
public class OrientJDBCDao {


    private Connection conn = null;

    public OrientJDBCDao() {

        Properties info = new Properties();
        info.put("user", "admin");
        info.put("password", "admin");

        try{
            this.conn = (OrientJdbcConnection) DriverManager.getConnection("jdbc:orient:remote:192.168.1.107/yagograph", info);
        }
        catch (Exception e){
            e.printStackTrace();
        }


    }


    public Connection getConn(){
        return this.conn;
    }








    public static void test_statement(Connection conn){

        Statement stmt = null;
        try {
            stmt = conn.createStatement();
        }
        catch (Exception e){
            e.printStackTrace();
        }

        List<String> deleteClass = new ArrayList<>();
        ResultSet rs = null;
        try{
            rs = stmt.executeQuery("select expand(classes) from metadata:schema");

            while( rs.next() ){
                String name = rs.getString("name");
                String superClass = rs.getString("superClass");
                System.out.println("[class = ] " + name + ", [superClass = ] " + superClass);
                if("E".equals(superClass) || "V".equals(superClass) )
                    deleteClass.add( name );
            }

            //rs.next();


        }
        catch (Exception e){
            e.printStackTrace();
        }


        for(String c : deleteClass){
            System.out.println("[delete = ]" + c);
            try{
                stmt.execute("delete Vertex " + c);
                stmt.execute("drop Class " + c);
            }
            catch (Exception e){
                e.printStackTrace();
            }
        }





    }


    public static void main(String[] args){


        OrientJDBCDao jdbc = new OrientJDBCDao();
        Connection conn = jdbc.getConn();


        test_statement( conn );

        System.out.println("OrientJDBCDao  exit!");


    }

}
